let App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '',
    time: '',
    week: '',
    shopInfo: {},
    merchants: {},
    lng: '',
    lat: '',
    service: [],
    price: 0,
    id: '',
    name: '',
    items: [],
    prompt: {
      hidden: !0,
      icon: '../../../assets/images/iconfont-empty.png'
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({shopInfo: options});
    this.shopDetail();
    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    this.initData()
    this.shopRevies(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let merchants = this.data.merchants;
    if (merchants.items.length === merchants.total) {
      return;
    }
    this.shopRevies();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  initData() {
    const merchants = {
      items: [],
      total: '',
      params: {
        page: 1,
        pageCount: 5
      }
    }
    this.setData({
      merchants: merchants
    })
  },
  // 打电话
  phoneCall() {
    wx.makePhoneCall({
      phoneNumber: this.data.shopInfo.phone,
    })
  },
  // 去导航
  toLocation() {
    wx.openLocation({
      latitude: parseFloat(this.data.lat),
      longitude: parseFloat(this.data.lng),
      scale: 18
    })
  },
  alert(msg) {
    wx.showToast({
      title: msg,
      image: '../../../assets/images/warning.png',
      duration: 1500
    })
  },
  // 跳转到提交订单页面
  toOrder () {
    console.log(this.data.shopInfo);
    if (this.data.price == 0) {
      this.alert("请选择商家服务");
      return false;
    }
    if (!this.data.date) {
      this.alert("请选择预约日期");
      return false;
    }
    if (!this.data.time) {
      this.alert("请选择预约时间");
      return false;
    }
    let merchantName = this.data.shopInfo.shopname;
    let phone = this.data.shopInfo.phone;
    let date = this.data.date;
    let time = this.data.time;
    let week = this.data.week;
    let address = this.data.shopInfo.address;
    let distance = this.data.shopInfo.distance;
    let price = this.data.price;
    let items = JSON.stringify(this.data.items);
    let shopId = this.data.shopInfo.shopId;
    let type = 4;
    let canshu = this.data.shopInfo.canshu
    wx.navigateTo({
      url: `/pages/order/washConfirm/washConfirm?merchantName=${merchantName}&phone=${phone}&date=${date}&time=${time}&week=${week}&address=${address}&distance=${distance}&price=${price}&items=${items}&shopId=${shopId}&canshu=${canshu}`,
    })
  },
  // 选择日期
  bindDateChange (e) {
    this.setData({
      date: e.detail.value
    })
    let date = new Date(e.detail.value);
    let day = date.getDay();
    switch (day) {
      case 0:
        this.setData({week:'周日'});
        break;
      case 1:
        this.setData({week: '周一'});
        break;
      case 2:
        this.setData({week: '周二'});
        break;
      case 3:
        this.setData({week: '周三'});
        break;
      case 4:
        this.setData({week: '周四'});
        break;
      case 5:
        this.setData({week: '周五'});
        break;
      case 6: 
        this.setData({week: '周六'});
        break;
      default:
        break;
    }
  },
  // 选择时间
  bindTimeChange (e) {

    this.setData({
      time: e.detail.value
    })
  },
  // 详情页面
  shopDetail () {
    let id = this.data.shopInfo.shopId;
    App.HttpService.washShopDetail({shopId:id}).then(res => {
      console.log(res);
      if (res.data.code == 200) {
        let data = res.data.data;
        this.setData({service: data.items, lng: data.shopInfo.lng, lat: data.shopInfo.lat});
      }
    });
  },
  // 商家评价
  shopRevies (cb) {
    let { merchants, shopInfo } = this.data;
    console.log(merchants);
    let shopId = shopInfo.shopId;
    let params = merchants.params;
    params.shopId = shopId;
    App.HttpService.shopRevies(params).then(res => {
      cb && cb();
      console.log(res);
      if (res.data.code == 200) {
        let data = res.data.data;
        merchants.items = [...merchants.items, ...data.revieList];
        if (data.revieList.length > 0) {
          params.page = params.page + 1;
          merchants.total = data.counts;
        }
        this.setData({ merchants, 'prompt.hidden': data.revieList.length });
      }
    });
  },
  // 选择商家服务
  chooseService (e) {
    console.log(e);
    let data = e.currentTarget.dataset;
    let id = data.id;
    let price = data.money;
    let name = data.name;
    let obj = {};
    obj.id = id;
    obj.price = price;
    obj.name = name;
    let items = [];
    items.push(obj);
    this.setData({price,id,name,items});
  },
  // 跳转到评价详情页面
  toComments (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/comments/detail/detail?id=${id}`,
    })
  }
})